package com.cookor.poi.modules.phy.mobile;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.modules.phy.entity.TArticle;
import com.cookor.poi.modules.phy.entity.TArticleType;
import com.cookor.poi.modules.phy.service.TArticleService;
import com.cookor.poi.modules.phy.service.TArticleTypeService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/phy/article"})
@ResponseBody
public class ApiArticleController extends ApiBaseController {
  @Autowired
  private TArticleService tArticleService;
  
  @Autowired
  private TArticleTypeService tArticleTypeService;
  
  @RequestMapping({"/type/list"})
  public ResultUtil typeList(HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      Integer tenantId = getTenantId(request);
      TArticleType articleType = new TArticleType();
      articleType.setDelFlag("0");
      articleType.setTenantId(tenantId);
      result.putParams("list", this.tArticleTypeService.findList(articleType));
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxArticleController typeList error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/list"})
  public ResultUtil articleList(Integer pageNo, Integer pageSize, Integer type, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      if (pageNo == null)
        pageNo = Integer.valueOf(1); 
      if (pageSize == null)
        pageSize = Integer.valueOf(5); 
      Integer tenantId = getTenantId(request);
      if (type == null)
        throw new Exception("参数有误"); 
      TArticle tArticle = new TArticle();
      tArticle.setTenantId(tenantId);
      tArticle.setDelFlag("0");
      tArticle.setType(type);
      result.putParams("page", this.tArticleService.findPage(new Page(pageNo.intValue(), pageSize.intValue()), tArticle));
      TArticleType tArticleType = new TArticleType();
      tArticleType.setTenantId(tenantId);
      tArticleType.setDelFlag("0");
      List<TArticleType> typeList = this.tArticleTypeService.findList(tArticleType);
      result.putParams("articleList", typeList);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxArticleController list error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/get"})
  public ResultUtil articleGet(Integer articleId, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      if (articleId == null)
        throw new Exception("参数错误"); 
      TArticle tArticle = this.tArticleService.get(articleId.toString());
      tArticle.setContent(tArticle.getContent().replaceAll("/userfiles/", "https://zx.nthink.cn/userfiles/"));
      result.putParams("res", tArticle);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxArticleController get error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/addHits"})
  public ResultUtil articleAddHits(Integer articleId, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      if (articleId == null)
        throw new Exception("参数错误"); 
      this.tArticleService.addHits(articleId.toString());
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxArticleController articleAddHits error!", e);
    } 
    return result;
  }
}
